<template>
<div>
    <el-form :inline="true" :model="house" class="demo-form-inline">
        <el-form-item label="楼栋选择">
            <el-select v-model="valuebuilding" placeholder="楼栋选择"@change="changeUnit">
                <el-option
                        v-for="item in buildingList"
                        :key="item.buildingName"
                        :label="item.buildingName"
                        :value="item.id"
                >
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="单元选择">
            <el-select v-model="valueUnit" placeholder="单元选择">
                <el-option
                        v-for="item in unitList"
                        :key="item.unitName"
                        :label="item.unitName"
                        :value="item.id"
                >
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="楼层选择" prop="floor">
        <el-input v-model="house.floor"></el-input>
        </el-form-item>
        <el-form-item label="门牌号" prop="houseNumber">
            <el-input v-model="house.houseNumber"  @blur="blurNumber"></el-input>
        </el-form-item>
        <el-form-item label="房屋面积" prop="houseArea">
            <el-input v-model="house.houseArea"></el-input>
        </el-form-item>
        <el-form-item label="物业费">
            <el-select v-model="valuefeeStandards" placeholder="物业费">
                <el-option
                        v-for="item in feeStandardsList"
                        :key="item.fees"
                        :label="item.fees+'/㎡/月'"
                        :value="item.id"
                >
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="状态">
            <el-select v-model="houseState.id" placeholder="状态">
                <el-option  v-for="item in houseStateOptions"
                            :key="item.state"
                            :label="item.state"
                            :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">添加</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button @click="offForm()">取消</el-button>
        </el-form-item>
    </el-form>
</div>
</template>

<script>
export default {
    name: "ManageHouseAddView",
    data() {
        return {
            buildingList: [],
            unitList:[],
            feeStandardsList:[],
            house:{},
            valuebuilding: '',
            valueUnit:'',
            valuefeeStandards:'',
            id:this.$route.query.id,
            houseStateOptions:[{}],
            houseState:{id:''},

    }
    },
    created() {
        this.findAllbuildings();
        this.chenangeFeeStandards();
        this.handleHouseStateOptions()
        if(this.id!=null){
            this.houseByid();
        }
    },
    methods:{
        onSubmit(){
            this.house.unitId=this.valueUnit;
            this.house.propertyFeesId = this.valuefeeStandards;
            this.house.houseStateId=this.houseState.id;
            this.house.id = this.id;
            this.house.buildingId=this.valuebuilding;
             console.log("================")
             console.log(this.house)
            if(!this.house.unitId){ this.$message.error("请选择单元"); return false; }
            else if(!this.house.houseNumber){this.$message.error("请输入门牌号"); return false;}
            else if(!this.house.floor){this.$message.error("请输入楼层"); return false;}
            else if(!this.house.houseArea){this.$message.error("请输入房屋面积"); return false;}
            else if(!this.house.propertyFeesId){this.$message.error("请选择物业缴费标准"); return false;}
            this.$axios.post("manage/housesave",this.house).then(res=>{
                this.$message.success(res.data.msg);
                this.house = {}
                this.$router.push("house")
            });


        },
        resetForm(formName) {
            this.valueUnit='';
            this.valuefeeStandards='';
            this.valueUnit='';
            this.valuebuilding='';
            this.house={};
        },
        findAllbuildings(){
            this.$axios.get("/manage/findbuilding").then(res=>{
              this.buildingList = res.data.buildingList;
            });
        },
        changeUnit(){
            this.$axios.get("manage/unit/findUnitBybuildingid",{params: {id:this.valuebuilding}}).then(res=>{
              this.unitList = [];
               this.unitList = res.data.units;
                this.valueUnit = this.unitList[0];
            });
        },
        chenangeFeeStandards(){
            this.$axios.get("manage/findAllFees").then(res=>{
                this.feeStandardsList = res.data.propertyFeesList;

            });
        },
        blurNumber(){
            this.house.unitId = this.valueUnit;

            this.$axios.post("manage/blurNumber",this.house).then(res=>{
                if(res.data.house!=null){
                    this.$message.error("此楼栋此单元此楼层门牌号已存在")
                }
            })
        },
        houseByid(){
            this.$axios.get("manage/houseByid?id="+this.id)
                .then(res=>{
                    console.log("=============修改=============")
                    console.log(res.data.house)
                    this.house = res.data.house;

                })
        },
        handleHouseStateOptions(){
            this.$axios.get("manage/findHouseStateOptions")
                .then(res=>{
                    if (res.data && res.data.findHouseStateOptions) {
                        this.houseStateOptions =res.data.findHouseStateOptions
                    }

                })
        },
        offForm(){
            this.$router.push("house");
        }
    },

}
</script>

<style scoped>

</style>